List<(A, B)> findMatchingPairs<A, B, T>(
  List<A> listA,
  List<B> listB,
  T Function(A) keyA,
  T Function(B) keyB,
) {
  // 先把 listB 按 key 分组存入 Map，加快查找
  final Map<T, List<B>> mapB = {};
  for (final b in listB) {
    final key = keyB(b);
    mapB.putIfAbsent(key, () => []).add(b);
  }

  final List<(A, B)> result = [];

  for (final a in listA) {
    final key = keyA(a);
    final matchingBs = mapB[key];
    if (matchingBs != null) {
      for (final b in matchingBs) {
        result.add((a, b)); // Dart 3 的 record 语法
      }
    }
  }

  return result;
}
